//
//  VisitorLoginView.swift
//  微博
//
//  Created by 吴思达 on 16/3/6.
//  Copyright © 2016年 吴思达. All rights reserved.
//

import UIKit
import SnapKit

@objc protocol VisitorLoginViewDelegate: NSObjectProtocol{
    
    optional func userWillLogin()
    func userWillRegister()
    
}

class VisitorLoginView: UIView {
    
   weak var visitorLoginViewDelegate: VisitorLoginViewDelegate?
    
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUpUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    @objc private func loginBtnDidClick() {
        visitorLoginViewDelegate?.userWillLogin!()
        
        
    }
    @objc private func registerBtnDidClick() {
        visitorLoginViewDelegate?.userWillRegister()
        
    }
    
    func setupInforWith(tipText: String,imageName: String?) {
        tipLabel.text = tipText
        if imageName != nil {
            circleIcon.image = UIImage(named: imageName!)
            LargeHouse.hidden = true
            self.bringSubviewToFront(circleIcon)
        }else{
            startAnimation()
        }
    }
    private func startAnimation() {
        let ani = CABasicAnimation(keyPath: "transform.rotation")
        ani.repeatCount = MAXFLOAT
        ani.toValue = 2 * M_PI
        ani.duration = 20
        ani.removedOnCompletion = false
        circleIcon.layer.addAnimation(ani, forKey: nil)
        
    }
    
    private func setUpUI() {
        addSubview(circleIcon)
        addSubview(backgroundImage)
        addSubview(LargeHouse)
        addSubview(tipLabel)
        addSubview(loginBtn)
        addSubview(registerBtn)
        
        
        circleIcon.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(-80)
        }
        LargeHouse.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        tipLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.centerY.equalTo(self.snp_centerY).offset(16)
            make.width.equalTo(230)
            make.height.equalTo(40)
        }
        loginBtn.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(tipLabel.snp_left)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        registerBtn.snp_makeConstraints { (make) -> Void in
            make.right.equalTo(tipLabel)
            make.width.equalTo(100)
            make.top.equalTo(tipLabel.snp_bottom).offset(16)
        }
        backgroundImage.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(circleIcon.snp_center)
        }
        self.backgroundColor = UIColor(white: 0.93, alpha: 1)
        
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
    }
    
    
    //懒加载
    private lazy var backgroundImage: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    private lazy var circleIcon: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    private lazy var LargeHouse: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    private lazy var tipLabel: UILabel = {
        let l = UILabel()
        l.text = "关注一些人,回到这里看看有什么惊喜"
        l.textColor = UIColor.darkGrayColor()
        l.font = UIFont.systemFontOfSize(14)
        l.numberOfLines = 0;
        l.textAlignment = .Center
        l.sizeToFit()
        return l
    }()
    
    private lazy var loginBtn: UIButton = {
        let btn = UIButton()
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        btn.setTitle("登录", forState: .Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        
        return btn
    }()
    
    private lazy var registerBtn: UIButton = {
        
        let btn = UIButton()
        let image: UIImage = UIImage(named: "common_button_white_disable")!
        let w = Int(image.size.width * 0.5)
        let h = Int(image.size.height * 0.5)
        btn.setBackgroundImage(image.stretchableImageWithLeftCapWidth(w, topCapHeight: h), forState: .Normal)
        btn.setTitleColor(UIColor.orangeColor(), forState: .Normal)
        btn.setTitle("注册", forState: .Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(14)
        btn.sizeToFit()
        return btn
    }()
}
